package org.agile4j.patterns;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Serialization {  
    public static void main(String [] args) {  
        Student stu1 = new Student(1, "Ronnie", 37, "snooker");  
        Student stu2 = new Student(2, "John", 37, "snooker");  
          
        try {  
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("student.txt")));  
              
            os.writeObject(stu1);  
            os.writeObject(stu2);  
              
            os.close();  
              
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(new File("student.txt")));  
              
            System.out.println((Student)is.readObject());  
            System.out.println((Student)is.readObject());  
              
            is.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  
  
class Student implements Serializable {  
	private static final long serialVersionUID = -2965249670550506475L;
//	private static final long serialVersionUID = 1L;
	int id;  
    String name;  
    int age;  
    String department;  
      
    public Student(int id, String name, int age, String department) {  
        this.id = id;  
        this.name = name;  
        this.age = age;  
        this.department = department;  
    }  
      
    public String toString() {  
        return "name:" + name + ", id:" + id + ", age:" + age + ", department:" + department;  
    }  
} 
